package ray.mgocc;

import java.util.HashMap;
import java.util.Map;
import java.text.ParseException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Score extends HashMap<String,Object>{
    private final static Log log = LogFactory.getLog(Score.class);

    private String name = null;
    private RuleScore rule = new RuleScore();
    public boolean rulefull = false;

    private String currentTitle = null;
    public boolean pushTitle(String title){
        title = title.trim();
        this.currentTitle = title;
        return true;
    }
    public boolean push(String value){
        // 先ずはruleに。一杯になってたらdetailに。
        if(rulefull){
            value = filter(value);
            Object obj = null; 
            if(value.matches("^[0-9]+$")){
                obj = Integer.parseInt(value);
            }else if(Time.match(value)){
                try{
                    obj = new Time(value);
                }catch(ParseException e){
                    log.error("not reach.",e);
                }
            }else{
                obj = value;
            }
            put(currentTitle,obj);
        }else{
            boolean notfull = rule.push(Integer.parseInt(value));
            rulefull = !notfull;
        }
        return true;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
    public String toString(){
        return getName()+"["+(isWeekly()?"WEEK":"TOTAL")+"]";
    }
    public RuleScore getRule(){
        return this.rule;
    }
    private boolean weekly = false;
    /**
     * @param weekly false:Cumulative Total(累計), true:Weekly Total(週計)
     */
    public void setWeekly(boolean weekly){
        this.weekly = weekly;
    }
    public boolean isWeekly(){
        return this.weekly;
    }
    public int getValue(String name){
        int number = 0;
        Object value = get(name);
        if(value instanceof Number){
            number = ((Number)value).intValue();
        }
        return number;
    }
    public Time getTime(String name){
        Time time = null;
        Object value = get(name);
        if(value instanceof Time){
            time = (Time)value;
        }
        return time;
    }
    private static String filter(String value){
        return value.replaceAll("[\\p{Blank}a-zA-Z]+","");
    }
    public boolean isRule(){
        return (null!=Rules.getShortName(getName()));
    }
    public Properties getAll(){
        Properties all = new Properties();
        for(RuleScore.Item item:rule){
            for(Map.Entry entry:item.entrySet()){
                all.setProperty(item.getName()+"."+entry.getKey().toString(),
                                entry.getValue().toString());
            }
        }
        for(Map.Entry entry:this.entrySet()){
            all.setProperty(entry.getKey().toString(),
                            entry.getValue().toString());
        }
        return all;
    }
}
